
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>JavaAgent · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3.3.html" />
    
    
    <link rel="prev" href="3.1.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" >
            
                <a target="_blank" href="https://github.com/xfl03/CoreModTutor">
            
                    
                    源代码仓库（求star）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="./">
            
                <a href="./">
            
                    
                    目录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="0.html">
            
                <a href="0.html">
            
                    
                    0 绪论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    1 简介
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="1.1.html">
            
                <a href="1.1.html">
            
                    
                    CoreMod
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="1.2.html">
            
                <a href="1.2.html">
            
                    
                    Java虚拟机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="1.3.html">
            
                <a href="1.3.html">
            
                    
                    Minecraft混淆方式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    2 Java虚拟机
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" >
            
                <span>
            
                    
                    ClassLoader类加载器
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" >
            
                <span>
            
                    
                    ByteCode字节码
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" >
            
                <span>
            
                    
                    ASM
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    3 原版 CoreMod
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="3.1.html">
            
                <a href="3.1.html">
            
                    
                    直接修改class文件
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6.2" data-path="3.2.html">
            
                <a href="3.2.html">
            
                    
                    JavaAgent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="3.3.html">
            
                <a href="3.3.html">
            
                    
                    LaunchWrapper
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="3.4.html">
            
                <a href="3.4.html">
            
                    
                    ModLauncher
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    4 FML CoreMod
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="4.1.html">
            
                <a href="4.1.html">
            
                    
                    1.3.2-1.5.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="4.2.html">
            
                <a href="4.2.html">
            
                    
                    1.6.1-1.12.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="4.3.html">
            
                <a href="4.3.html">
            
                    
                    1.13.2-1.14.4
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="5.html">
            
                <a href="5.html">
            
                    
                    5 Mixin
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="5.1.html">
            
                <a href="5.1.html">
            
                    
                    配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="5.2.html">
            
                <a href="5.2.html">
            
                    
                    引导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="5.3.html">
            
                <a href="5.3.html">
            
                    
                    注入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="5.4.html">
            
                <a href="5.4.html">
            
                    
                    修改
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="5.5.html">
            
                <a href="5.5.html">
            
                    
                    定位
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="5.6.html">
            
                <a href="5.6.html">
            
                    
                    融合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="5.7.html">
            
                <a href="5.7.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="5.8.html">
            
                <a href="5.8.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="附录A.html">
            
                <a href="附录A.html">
            
                    
                    附录A 相关工具下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="附录B.html">
            
                <a href="附录B.html">
            
                    
                    附录B 常见Java字节码指令表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="附录C.html">
            
                <a href="附录C.html">
            
                    
                    附录C 参考资料
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >JavaAgent</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="javaagent">JavaAgent</h1>
<p>&#x867D;&#x7136;FML&#x4ECE;&#x672A;&#x4F7F;&#x7528;&#x8FC7;JavaAgent&#x6765;&#x5BF9;Minecraft&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x4E5F;&#x662F;&#x4E00;&#x79CD;&#x5408;&#x7406;&#x7684;&#x52A8;&#x6001;&#x4FEE;&#x6539;&#x65B9;&#x5F0F;&#x3002;<br>JavaAgent&#x7531;JVM&#x8FDB;&#x884C;&#x652F;&#x6301;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;class&#x6587;&#x4EF6;&#x88AB;&#x52A0;&#x8F7D;&#x65F6;&#x5BF9;&#x5176;&#x8FDB;&#x884C;&#x52A8;&#x6001;&#x7684;&#x4FEE;&#x6539;&#x3002;</p>
<h2 id="&#x5236;&#x4F5C;&#x65B9;&#x6CD5;">&#x5236;&#x4F5C;&#x65B9;&#x6CD5;</h2>
<p>JavaAgent&#x7531;&#x4EE5;&#x4E0B;&#x51E0;&#x90E8;&#x5206;&#x7EC4;&#x6210;&#x2014;&#x2014;<code>premain</code>&#x65B9;&#x6CD5;&#x3001;<code>ClassFileTransformer</code>&#x63A5;&#x53E3;&#x3002;</p>
<h3 id="premain">premain</h3>
<p><code>premain</code>&#x65B9;&#x6CD5;&#x4F1A;&#x5728;Java&#x7684;&#x5165;&#x53E3;&#x65B9;&#x6CD5;<code>main</code>&#x524D;&#x6267;&#x884C;&#x3002;<br>&#x4E00;&#x4E2A;&#x5178;&#x578B;&#x7684;&#x5199;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.example;

<span class="hljs-keyword">import</span> java.lang.instrument.Instrumentation;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExampleAgent</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">premain</span><span class="hljs-params">(String args, Instrumentation instrumentation)</span></span>{
      <span class="hljs-comment">//<span class="hljs-doctag">TODO:</span> &#x5728;&#x6B64;&#x8865;&#x5145;&#x4EE3;&#x7801;</span>
  }
}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x83B7;&#x5F97;&#x4E00;&#x4E2A;<code>Instrumentation</code>&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x901A;&#x8FC7;&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6CE8;&#x518C;<code>ClassFileTransformer</code>&#x3002;</p>
<pre><code class="lang-java">ExampleTransformer transformer = <span class="hljs-keyword">new</span> ExampleTransformer();
instrumentation.addTransformer(transformer);
</code></pre>
<p><code>premain</code>&#x65B9;&#x6CD5;&#x548C;<code>main</code>&#x65B9;&#x6CD5;&#x4E00;&#x6837;&#xFF0C;&#x9700;&#x8981;&#x5199;&#x5165;Manifest&#x624D;&#x80FD;&#x4F7F;JVM&#x6267;&#x884C;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4FEE;&#x6539;<code>gradle.build</code>&#x7684;&#x65B9;&#x5F0F;&#x52A0;&#x5165;&#xFF1A;</p>
<pre><code class="lang-gradle">jar {
    manifest {
        attributes([
                <span class="hljs-string">&quot;Premain-Class&quot;</span>: <span class="hljs-string">&quot;com.example.ExampleAgent&quot;</span>
        ])
    }
</code></pre>
<h3 id="classfiletransformer">ClassFileTransformer</h3>
<p>&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;<code>transform</code>&#xFF0C;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x4FEE;&#x6539;&#x5B8C;&#x540E;&#x7684;class&#x6587;&#x4EF6;<code>byte</code>&#x6570;&#x7EC4;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4F1A;&#x5728;<code>ClassLoader</code>&#x7684;<code>defineClass</code>&#x65F6;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x53C2;&#x6570;&#x5982;&#x4E0B;&#xFF1A;</p>
<ul>
<li><code>loader</code>&#xFF0C;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x7684;<code>ClassLoader</code></li>
<li><code>className</code>&#xFF0C;&#x5F53;&#x524D;&#x52A0;&#x8F7D;&#x7684;class&#x540D;&#x79F0;</li>
<li><code>classBeingRedefined</code>&#xFF0C;&#x4E3A;retransform&#x8BBE;&#x8BA1;&#xFF0C;&#x91CD;&#x65B0;&#x52A0;&#x8F7D;&#x524D;&#x7684;<code>Class</code>&#x5BF9;&#x8C61;</li>
<li><code>protectionDomain</code>&#xFF0C;&#x4FDD;&#x62A4;&#x7684;&#x57DF;</li>
<li><code>classfileBuffer</code>&#xFF0C;class&#x6587;&#x4EF6;&#x7684;<code>byte</code>&#x6570;&#x7EC4;</li>
</ul>
<p>&#x4E00;&#x4E2A;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x4F5C;&#x7528;&#x7684;<code>ClassFileTransformer</code>&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.example;

<span class="hljs-keyword">import</span> java.lang.instrument.ClassFileTransformer;
<span class="hljs-keyword">import</span> java.lang.instrument.IllegalClassFormatException;
<span class="hljs-keyword">import</span> java.security.ProtectionDomain;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExampleTransformer</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">ClassFileTransformer</span> </span>{
    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">byte</span>[] transform(ClassLoader loader,
                            String className,
                            Class&lt;?&gt; classBeingRedefined,
                            ProtectionDomain protectionDomain,
                            <span class="hljs-keyword">byte</span>[] classfileBuffer) <span class="hljs-keyword">throws</span> IllegalClassFormatException {
        <span class="hljs-comment">//<span class="hljs-doctag">TODO:</span> &#x53EF;&#x4EE5;&#x5728;&#x6B64;&#x5199;ASM&#x4EE3;&#x7801;</span>
        <span class="hljs-keyword">return</span> classfileBuffer;
    }
}
</code></pre>
<h2 id="&#x5B89;&#x88C5;&#x65B9;&#x6CD5;">&#x5B89;&#x88C5;&#x65B9;&#x6CD5;</h2>
<p>&#x5728;&#x6E38;&#x620F;&#x7684;JVM&#x53C2;&#x6570;&#x4E2D;&#x52A0;&#x5165;&#xFF1A;</p>
<pre><code class="lang-sh">-javaagent:filename=args
</code></pre>
<ul>
<li><code>filename</code>&#x4E3A;jar&#x7684;&#x6587;&#x4EF6;&#x540D;</li>
<li><code>args</code>&#x4E3A;&#x4F20;&#x5165;<code>premain</code>&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;</li>
</ul>
<h2 id="&#x5F0A;&#x7AEF;">&#x5F0A;&#x7AEF;</h2>
<ul>
<li>&#x9700;&#x8981;&#x7528;&#x6237;&#x4FEE;&#x6539;JVM&#x53C2;&#x6570;&#x6765;&#x4F7F;&#x7528;JavaAgent&#xFF0C;&#x76F8;&#x5BF9;&#x4E8E;&#x5176;&#x4ED6;&#x5B89;&#x88C5;&#x65B9;&#x6CD5;&#x8F83;&#x4E3A;&#x9EBB;&#x70E6;</li>
<li>&#x4F18;&#x5148;&#x7EA7;&#x8FC7;&#x4F4E;&#xFF0C;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#x90FD;&#x662F;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5BF9;&#x7C7B;&#x8FDB;&#x884C;&#x4FEE;&#x6539;</li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="3.1.html" class="navigation navigation-prev " aria-label="Previous page: 直接修改class文件">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3.3.html" class="navigation navigation-next " aria-label="Next page: LaunchWrapper">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"JavaAgent","level":"1.6.2","depth":2,"next":{"title":"LaunchWrapper","level":"1.6.3","depth":2,"path":"3.3.md","ref":"3.3.md","articles":[]},"previous":{"title":"直接修改class文件","level":"1.6.1","depth":2,"path":"3.1.md","ref":"3.1.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"3.2.md","mtime":"2019-03-09T16:36:16.985Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-09-08T04:47:05.818Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

